package com.cmas.platform.controller;

import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.aidex.common.annotation.DataSource;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.TableDataInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.core.controller.BaseController;
import com.aidex.framework.cache.DictUtils;

import javax.validation.constraints.*;

import com.cmas.platform.vo.SectionVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmas.platform.domain.Section;
import com.cmas.platform.service.SectionService;


/**
 * 版块管理Controller
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-14
 */
@DataSource(value = DataSourceType.SLAVE)
@RestController
@RequestMapping("/platform/section")
public class SectionController extends BaseController {

    @Autowired
    private SectionService sectionService;


    /**
     * 查询版块管理列表
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:section:list,platform:region:list')")
    @GetMapping("/list")
    public R<TableDataInfo> list(Section section) {
        startPage();
        List<SectionVo> list = sectionService.selectSectionVoList(section);
        return R.data(getDataTable(list));
    }

    /**
     * 获取版块管理详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:section:query,platform:region:query')")
    @GetMapping(value = "/{id}")
    public R<Section> detail(@PathVariable("id") String id) {
        return R.data(sectionService.getById(id));
    }

    /**
     * 新增版块管理
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:section:add,platform:region:edit')")
    @Log(title = "版块管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Section section) {
        return R.data(sectionService.save(section));
    }

    /**
     * 修改版块管理
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:section:edit,platform:region:edit')")
    @Log(title = "版块管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Section section) {
        return R.data(sectionService.updateById(section));
    }

    /**
     * 删除版块管理
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:section:remove,platform:region:remove')")
    @Log(title = "版块管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.data(sectionService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:section:query,platform:region:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort(Section section) {
        return R.data(sectionService.findMaxSort(section));
    }

    /**
     * 校验版块编码的唯一性
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:section:query,platform:region:query')")
    @Log(title = "版块管理", businessType = BusinessType.CHECK)
    @GetMapping(value = {"/checkCodeUnique/{code}/{id}", "/checkCodeUnique/{code}"})
    public R checkCodeUnique(@NotBlank(message = "版块编码不允许为空") @PathVariable("code") String code, @PathVariable(value = "id", required = false) String id) {
        Section section = new Section();
        section.setCode(code);
        section.setId(id);
        return R.data(sectionService.checkCodeUnique(section));
    }


    /**
     * 导出版块管理列表
     */
    @PreAuthorize("@ss.hasPermi('platform:section:export')")
    @Log(title = "版块管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Section section) {
        List<Section> list = sectionService.selectSectionList(section);
        ExcelUtil<Section> util = new ExcelUtil<Section>(Section.class);
        return util.exportExcel(list, "版块管理数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
